<?php

namespace app\data\controller\api;

use app\data\service\AliMessageService;
use think\admin\Controller;

/**
 * 短信接口
 * Class Sms
 * @package app\data\controller\api
 */
class Sms extends Controller
{
    /**
     * 发送短信验证码
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function send()
    {
        $data = $this->_vali([
            'phone.mobile'   => '手机号格式错误！',
            'phone.require'  => '手机号不能为空！',
            'type.default'  => 'register',
        ]);
        [$state, $message, $data] = AliMessageService::instance()->sendVerifyCode($data['phone'], 120, $data['type']);
        $state ? $this->success($message, $data) : $this->error($message, $data);
    }

    /**
     * 短信验证码校验
     */
    public function check()
    {
        $data = $this->_vali([
            'phone.mobile'   => '手机号格式错误！',
            'phone.require'  => '手机号不能为空！',
            'type.default'  => 'register',
            'verify.require'  => '验证码不能为空',
        ]);

        if (!AliMessageService::instance()->checkVerifyCode($data['verify'], $data['phone'], $data['type'])) {
            $this->error('短信验证失败！');
        }

        $this->success('短信验证成功！');
    }
}